package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotMonitorDataService;
import com.lechi.web.system.domain.IotMonitorDataBo;
import com.lechi.web.system.domain.IotMonitorDataCountBo;
import com.lechi.web.system.domain.IotSensorValue;
import com.lechi.web.system.mapper.iot.IotMonitorDataMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;

/**
 * 监测数据Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotMonitorDataServiceImpl implements IIotMonitorDataService {

    private final IotMonitorDataMapper iotMonitorDataMapper;

    private final ForkJoinPool forkJoinPool = new ForkJoinPool();

    /**
     * 查询监测数据列表
     *
     * @param iotMonitorDataBo 监测数据
     * @return 监测数据
     */
    @Override
    public List<IotMonitorDataBo> selectIotMonitorDataList(IotMonitorDataBo iotMonitorDataBo) {
        try {
            return iotMonitorDataMapper.selectIotMonitorDataList(iotMonitorDataBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询监测数据列表", method, iotMonitorDataBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询导出监测数据列表
     *
     * @param iotMonitorDataBo 监测数据
     * @return 监测数据
     */
    @Override
    public List<IotMonitorDataBo> exportIotMonitorDataList(IotMonitorDataBo iotMonitorDataBo) {
        try {
            return iotMonitorDataMapper.exportIotMonitorDataList(iotMonitorDataBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "导出监测数据列表", method, iotMonitorDataBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 格式化导出监测数据列表
     *
     * @param map 监测数据
     * @return 监测数据
     */
    @Override
    public Map<String, IotSensorValue> formatTimeExportMonitorData(int queryType, Map<String, Object> map) {
        Map<String, IotSensorValue> mapResult = null;
        try {
            switch (queryType) {
                case 1 -> mapResult = iotMonitorDataMapper.selectRealTimeExportMonitorData(map);
                case 2 -> mapResult = iotMonitorDataMapper.selectMaXExportMonitorData(map);
                case 3 -> mapResult = iotMonitorDataMapper.selectMinExportMonitorData(map);
                case 4 -> mapResult = iotMonitorDataMapper.selectSumExportMonitorData(map);
                case 5 -> mapResult = iotMonitorDataMapper.selectAvgExportMonitorData(map);
                default -> mapResult = new HashMap<>();
            }
//            if (queryType == 1) {
//                mapResult = iotMonitorDataMapper.selectRealTimeExportMonitorData(map);
//            }
//            if (queryType == 2) {
//                mapResult = iotMonitorDataMapper.selectMaXExportMonitorData(map);
//            }
//            if (queryType == 3) {
//                mapResult = iotMonitorDataMapper.selectMinExportMonitorData(map);
//            }
//            if (queryType == 4) {
//                mapResult = iotMonitorDataMapper.selectSumExportMonitorData(map);
//            }
//            if (queryType == 5) {
//                mapResult = iotMonitorDataMapper.selectAvgExportMonitorData(map);
//            }
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询平均，最大，最小，求和监测数据列表", method, map));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
        return mapResult;
    }

    /**
     * 数据分析监测数据列表
     *
     * @param map 监测数据
     * @return 监测数据
     */
    @Override
    public List<IotSensorValue> selectDataAnalysisData(Map<String, Object> map) {
        List<IotSensorValue> iotSensorValues = null;
        try {
            if (Objects.equals(1, map.get("queryType"))) {
                iotSensorValues = iotMonitorDataMapper.selectRealDataAnalysisData(map);
            } else {
                iotSensorValues = iotMonitorDataMapper.selectDataAnalysisData(map);
            }
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "数据分析监测数据", method, map));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
        return iotSensorValues;
    }

    /**
     * 统计监测数据列表
     *
     * @param iotMonitorDataBo 监测数据
     * @return 监测数据
     */
    @Override
    public IotMonitorDataCountBo selectIotMonitorDataCount(IotMonitorDataBo iotMonitorDataBo) {
        try {
            return iotMonitorDataMapper.selectIotMonitorDataCount(iotMonitorDataBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "统计监测数据列表", method, iotMonitorDataBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增监测数据
     *
     * @param iotMonitorDataBo 监测数据
     */
    @Override
    public void insertIotMonitorData(String tableName, IotMonitorDataBo iotMonitorDataBo) {
        try {
            iotMonitorDataMapper.insertIotMonitorData(tableName, iotMonitorDataBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增监测数据", method, iotMonitorDataBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 新增监测数据
     *
     * @param list 监测数据
     */
    @Override
    public void batchInsert(String tableName, List<IotMonitorDataBo> list) {
        try {
            iotMonitorDataMapper.batchInsert(tableName, list);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批处理入库监测数据", method, list.size()));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 批量删除监测数据
     *
     * @param ids 需要删除的监测数据主键
     */
    @Override
    public void deleteIotMonitorDataByIds(Long[] ids) {
        try {
            iotMonitorDataMapper.deleteIotMonitorDataByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除监测数据", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

}
